package com.cug.zfanspring.controller;



import com.cug.zfanspring.bean.User;
import com.cug.zfanspring.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class UserController {
    @Autowired
    UserService userService;

    @RequestMapping("/user/login_page")
    public Map<String, String> loginPage() {
        Map<String, String> map = new HashMap<>();
         map.put("error", "尚未登录，请登录!");
         return map;
    }

    @RequestMapping(value ="/user/login")
    @ResponseBody
    public Map<String, String> login(String name, String password) {
        int result = userService.login(name,password);
        Map<String, String> map = new HashMap<>();
        if (result == 0) {
            //成功
           map.put("success", "登录成功!");
        } else if (result == 1) {
            map.put("error", "用户名不存在，登录失败!");
        } else if (result == 2) {
            map.put("error", "用户名被锁，登录失败!");
        } else if (result == 3) {
            map.put("error", "密码错误!");
        }
        else{
            map.put("error","登录失败！");
        }
        return map;
    }

    @RequestMapping("/user/reg")
    @ResponseBody
    public Map<String, String> reg(String name, String password) {
        int result = userService.reg(name,password);
        Map<String, String> map = new HashMap<>();
        if (result == 0) {
            //成功
            map.put("success", "注册成功!");
        } else if (result == 1) {
            map.put("error", "用户名重复，注册失败!");
        } else {
            //失败
            map.put("error", "注册失败!");
        }
        return map;
    }
    @RequestMapping("/user/editPassword")
    @ResponseBody
    public Map<String, String> editPassword(String name,String password,String newPassword){
        int result = userService.login(name,password);
        Map<String, String> map = new HashMap<>();
        if (result == 0) {
            //成功
             int res=userService.editPassword(name,newPassword);
             if (res==1){
                 map.put("success","修改成功");
             }
             else if (res==2){
                 map.put("error","修改失败!");
             }
        } else if (result == 1) {
            map.put("error", "用户名不存在，修改失败!");
        } else if (result == 2) {
            map.put("error", "用户名被锁，修改失败!");
        } else if (result == 3) {
            map.put("error", "密码错误!");
        }
        else{
            map.put("error","登录失败！");
        }
        return map;
    }

    @RequestMapping("/user")
    @ResponseBody
    public User GPersonalInformation(String username){
       return userService.GetPersonalInformation(username);
    }





//
}
